﻿using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;

namespace SmallBasicFun
{
    public static class ProcessUtil
    {
        private static string _smallBasicProcessName = "SB.exe";

        public static string GetSmallBasicExecutionPath()
        {
            ManagementObjectCollection mcol = new ManagementClass("Win32_Process").GetInstances();

            foreach ( ManagementObject strt in mcol)
            {
                
                if (_smallBasicProcessName.Equals(strt["Name"].ToString()))
                {
                    var path = strt["ExecutablePath"].ToString();
                    return Directory.GetParent(path).FullName; 
                }
            }

            return null;
        }

        public static Process GetSmallBasicProcess()
        {
            var process = Process.GetProcessesByName("SB").FirstOrDefault();
            return process;
        }
    }
}
